<template>
  <div class="dashboard-container">
    <div class="dashboard-text">name: {{ name }}</div>
    <input id="fileSelector" type="file" @change="upload">
    <img :src="res" alt="">
    <hr>
    <el-upload
      :on-change="handleChange"
      :limit="3"
      action="#"
      list-type="picture-card"
      :auto-upload="false"
      :file-list="fileList"
      :class="{ hidden: fileList.length === 3 }"
    >
      <i slot="default" class="el-icon-plus" />
      <div slot="file" slot-scope="{ file }">
        <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
        <span class="el-upload-list__item-actions">
          <span
            class="el-upload-list__item-preview"
            @click="handlePictureCardPreview(file)"
          >
            <i class="el-icon-zoom-in" />
          </span>
          <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleRemove(file)"
          >
            <i class="el-icon-delete" />
          </span>
        </span>
      </div>
    </el-upload>
    <el-progress v-show="percent > 0" :percentage="percent" />
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <hr>
    <UploadImage ref="upload" :limit="3" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import COS from 'cos-js-sdk-v5'
// SECRETID 和 SECRETKEY 请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
var cos = new COS({
  SecretId: 'AKIDzpTs9C1bx62kDD4nMlxwkOp48ygBSU3S',
  SecretKey: 'FHE7yiLm01qUu78wWs071g8OZWFtMbbL'
})
export default {
  name: 'Dashboard',
  data() {
    return {
      res: '',
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      fileList: [],
      percent: 0
    }
  },
  computed: {
    ...mapGetters(['name'])
  },
  mounted() {
    this.$refs.upload.list = []
  },
  methods: {
    async upload(e) {
      const file = e.target.files && e.target.files[0]
      /* 直接调用 cos sdk 的方法 */
      const res = await cos.uploadFile({
        Bucket: 'super-1316543954' /* 填写自己的 bucket，必须字段 */,
        Region: 'ap-shanghai' /* 存储桶所在地域，必须字段 */,
        Key:
          Date.now() +
          e.target.files[0]
            .name /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */,
        Body: file, // 上传文件对象
        SliceSize:
          1024 *
          1024 *
          5 /* 触发分块上传的阈值，超过5MB 使用分块上传，小于5MB使用简单上传。可自行设置，非必须 */
      })
      console.log(res)
      this.res = 'https://' + res.Location
    },
    handleRemove(file) {
      console.log(file)
      const index = this.fileList.findIndex((item) => item.uid === file.uid)
      this.fileList.splice(index, 1)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleDownload(file) {
      console.log(file)
    },
    async handleChange(file, fileList) {
      console.log(file)
      const res = await cos.uploadFile({
        Bucket: 'super-1316543954' /* 填写自己的 bucket，必须字段 */,
        Region: 'ap-shanghai' /* 存储桶所在地域，必须字段 */,
        Key:
          Date.now() +
          file.raw
            .name /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */,
        Body: file.raw, // 上传文件对象
        SliceSize:
          1024 *
          1024 *
          5 /* 触发分块上传的阈值，超过5MB 使用分块上传，小于5MB使用简单上传。可自行设置，非必须 */,
        onProgress: (data) => {
          console.log(data)
          this.percent = Math.ceil(data.percent * 100)
        }
      })
      console.log(res)
      this.fileList.push({
        name: res.Location,
        url: 'https://' + res.Location
      })
      setTimeout(() => {
        this.percent = 0
      }, 1000)
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
::v-deep .hidden .el-upload--picture-card {
  display: none !important;
}
::v-deep .el-upload-list__item.is-ready {
  display: none;
}
</style>
